using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using WIT.DAQ.Core.Helper;
using WIT.DAQ.Core.Models;

namespace WIT.DAQ.Core.Forms;

/// <summary>
///
/// </summary>
internal class LaunchForm : BaseForm
{
	/// <summary>
	///
	/// </summary>
	/// <param name="stations">启动的站点</param>
	/// <param name="autoLaunch">是否为自动启动</param>
	internal delegate void LaunchHandler(List<Station> stations, bool autoLaunch);

	/// <summary>
	/// Required designer variable.
	/// </summary>
	private IContainer components = null;

	private TreeView tvStations;

	private Button btnConfirm;

	private Button btnCancel;

	private CheckBox cbCheckAll;

	/// <summary>
	///
	/// </summary>
	internal event LaunchHandler Launch;

	/// <summary>
	///
	/// </summary>
	internal LaunchForm()
	{
		InitializeComponent();
	}

	private void LaunchForm_Load(object sender, EventArgs e)
	{
		InitStations();
		string text = INIHelper.ReadValue("LaunchStations", "Ids", Common.MY_INI_PATH);
		if (string.IsNullOrEmpty(text) || text.ToUpper() == "ALL")
		{
			cbCheckAll.Checked = true;
			return;
		}
		string[] source = text.Split(',');
		int num = 0;
		foreach (TreeNode node in tvStations.Nodes)
		{
			if (source.Contains(node.Name))
			{
				node.Checked = true;
				num++;
			}
		}
		if (num == tvStations.Nodes.Count)
		{
			cbCheckAll.Checked = true;
		}
	}

	private void LaunchForm_Shown(object sender, EventArgs e)
	{
		tvStations.SelectedNode = null;
	}

	private void InitStations()
	{
		tvStations.Nodes.Clear();
		List<Station> allStations = Common.GetAllStations();
		foreach (Station item in allStations)
		{
			TreeNode node = new TreeNode
			{
				Name = item.Id.ToString(),
				Text = item.Name,
				ToolTipText = item.Desc,
				Tag = item
			};
			tvStations.Nodes.Add(node);
		}
	}

	/// <summary>
	/// 全选/取消全选
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void cbCheckAll_CheckedChanged(object sender, EventArgs e)
	{
		foreach (TreeNode node in tvStations.Nodes)
		{
			node.Checked = cbCheckAll.Checked;
		}
	}

	/// <summary>
	/// 确认
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnConfirm_Click(object sender, EventArgs e)
	{
		List<Station> list = new List<Station>();
		foreach (TreeNode node in tvStations.Nodes)
		{
			if (node.Checked)
			{
				list.Add((Station)node.Tag);
			}
		}
		if (list.Count == 0)
		{
			MessageBox.Show("请至少勾选一个站点启动采集", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return;
		}
		if (!File.Exists(Common.MY_INI_PATH))
		{
			FileStream fileStream = File.Create(Common.MY_INI_PATH);
			fileStream.Close();
		}
		string empty = string.Empty;
		empty = ((list.Count != tvStations.Nodes.Count) ? string.Join(",", list.Select((Station t) => t.Id)) : "ALL");
		INIHelper.WriteValue("LaunchStations", "Ids", empty, Common.MY_INI_PATH);
		this.Launch(list, autoLaunch: false);
		Close();
	}

	/// <summary>
	/// 关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnCancel_Click(object sender, EventArgs e)
	{
		Close();
	}

	/// <summary>
	/// ESC关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void LaunchForm_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Escape)
		{
			btnCancel_Click(null, null);
		}
	}

	/// <summary>
	/// Clean up any resources being used.
	/// </summary>
	/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
	{
		this.tvStations = new System.Windows.Forms.TreeView();
		this.btnConfirm = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.cbCheckAll = new System.Windows.Forms.CheckBox();
		base.SuspendLayout();
		this.tvStations.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.tvStations.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
		this.tvStations.CheckBoxes = true;
		this.tvStations.ItemHeight = 15;
		this.tvStations.Location = new System.Drawing.Point(5, 5);
		this.tvStations.Name = "tvStations";
		this.tvStations.ShowNodeToolTips = true;
		this.tvStations.Size = new System.Drawing.Size(342, 415);
		this.tvStations.TabIndex = 0;
		this.btnConfirm.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnConfirm.Location = new System.Drawing.Point(191, 426);
		this.btnConfirm.Name = "btnConfirm";
		this.btnConfirm.Size = new System.Drawing.Size(75, 23);
		this.btnConfirm.TabIndex = 2;
		this.btnConfirm.Text = "启动(&S)";
		this.btnConfirm.UseVisualStyleBackColor = true;
		this.btnConfirm.Click += new System.EventHandler(btnConfirm_Click);
		this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnCancel.Location = new System.Drawing.Point(272, 426);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 3;
		this.btnCancel.Text = "取消(&C)";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.cbCheckAll.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left;
		this.cbCheckAll.AutoSize = true;
		this.cbCheckAll.Location = new System.Drawing.Point(5, 426);
		this.cbCheckAll.Name = "cbCheckAll";
		this.cbCheckAll.Size = new System.Drawing.Size(102, 16);
		this.cbCheckAll.TabIndex = 1;
		this.cbCheckAll.Text = "全选/取消全选";
		this.cbCheckAll.UseVisualStyleBackColor = true;
		this.cbCheckAll.CheckedChanged += new System.EventHandler(cbCheckAll_CheckedChanged);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(351, 461);
		base.Controls.Add(this.cbCheckAll);
		base.Controls.Add(this.btnConfirm);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.tvStations);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "LaunchForm";
		this.Text = "启动选项 - 勾选需要启动任务的站点";
		base.Load += new System.EventHandler(LaunchForm_Load);
		base.Shown += new System.EventHandler(LaunchForm_Shown);
		base.KeyDown += new System.Windows.Forms.KeyEventHandler(LaunchForm_KeyDown);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
